﻿//--------------------------------------------------------------------------------------------------------------------------------------------------------
// <copyright file="ActivatingAUser.cs">
//     Copyright © 2010 Oli Francis
//     This source code is subject to the terms and conditions of the MIT license. A copy of the license can be found in the license.txt 
//     file at the root of this distribution. By using this source code in any fashion, you are agreeing to be bound by the terms of the MIT License. 
//     You must not remove this notice from the software.
// </copyright>
// <author>Oli</author>
//-------------------------------------------------------------------------------------------------------------------------------------------------------

namespace TeamBrain.Tests.Units.Registration
{
    using System;

    using Moq;

    using NUnit.Framework;

    using TeamBrain.Domain.Model;
    using TeamBrain.Domain.Services.Exceptions;

    [TestFixture]
    [Category("Units")]
    [Category("Registration")]
    public class ActivatingAUser : RegistrationAction
    {
        [Test]
        public void ShouldIgnoreIfAlreadyActive()
        {
            // arrange
            var user = new User { IsActive = true };
            this.UserRepository.Setup(r => r.GetByActivationToken(It.IsAny<string>())).Returns(user);

            // act
            this.RegistrationService.ActivateUserFromToken("tokenvalue");

            // assert
            Assert.AreEqual(true, user.IsActive);
            this.UserRepository.Verify(r => r.Update(It.IsAny<User>()), Times.Never());
        }

        [Test]
        [ExpectedException(typeof(InvalidActivationTokenException))]
        public void ShouldThrowExceptionIfActivationTokenIsNotRecognised()
        {
            this.RegistrationService.ActivateUserFromToken("tokenvalue");
        }

        [Test]
        public void ShouldMarkUserAccountAsActivated()
        {
            // arrange
            var user = new User { IsActive = false };
            this.UserRepository.Setup(r => r.GetByActivationToken(It.IsAny<string>())).Returns(user);

            // act
            this.RegistrationService.ActivateUserFromToken("tokenvalue");

            // assert
            Assert.AreEqual(true, user.IsActive);
            this.UnitOfWork.Verify(r => r.Flush());
        }

        [Test]
        public void ShouldSignInTheUser()
        {
            // arrange
            var user = new User { IsActive = false };
            this.UserRepository.Setup(r => r.GetByActivationToken(It.IsAny<string>())).Returns(user);

            // act
            this.RegistrationService.ActivateUserFromToken("tokenvalue");

            // assert
            this.SecurityService.Verify(s => s.SignInActivatedUser(It.IsAny<int>()));
        }

        [Test]
        public void ShouldNotSignInTheUserIfUserIsAlreadyActive()
        {
            // arrange
            var user = new User { IsActive = true };
            this.UserRepository.Setup(r => r.GetByActivationToken(It.IsAny<string>())).Returns(user);

            // act
            this.RegistrationService.ActivateUserFromToken("tokenvalue");

            // assert
            this.SecurityService.Verify(s => s.SignInActivatedUser(It.IsAny<int>()), Times.Never());
        }

        [Test]
        public void ShouldClearOutActivationTokenAfterSuccessfulActivation()
        {
            // arrange
            var user = new User { ActivationToken = "ABC", IsActive = false };
            this.UserRepository.Setup(r => r.GetByActivationToken(It.IsAny<string>())).Returns(user);

            // act
            this.RegistrationService.ActivateUserFromToken("tokenvalue");

            // assert
            Assert.IsNull(user.ActivationToken);
            this.UnitOfWork.Verify(x => x.Flush());
        }
    }
}
